package com.sgb.merchant.view.adapter;

import android.graphics.Color;
import android.util.TypedValue;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.utils.TextUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fondesa.recyclerviewdivider.DividerDecoration;
import com.sgb.merchant.databinding.MerchantInfoHeaderBinding;
import com.sgb.merchant.view.ApplyDialog;
import com.swgk.core.base.model.entity.SingleItemConstant;
import com.swgk.core.base.model.entity.SingleItemEntity;
import com.sgb.link.entity.merchant.EnrolInfoEntiy;
import com.sgb.link.entity.merchant.InvestmentDetailEntity;
import com.swgk.core.dialog.ImagesPreviewDialog;
import com.sgb.merchant.R;
import com.swgk.core.dialog.ImagesAdapter;

import java.util.ArrayList;
import java.util.List;

public class InvestmentDetailAdapter extends BaseMultiItemQuickAdapter<SingleItemEntity, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    ImagesAdapter picAdapter;
    EnrolListAdapter mListAdapter;
    toMoreClick toMoreClick;
    private boolean isFrist=true;
    String p_tag = "";
    List<String> urls;
    boolean isSelf;//是否自己的详情页
    View.OnClickListener onClickListener;
    public void setSelf(boolean self) {
        isSelf = self;
    }

    public void setToMoreClick(InvestmentDetailAdapter.toMoreClick toMoreClick) {
        this.toMoreClick = toMoreClick;
    }

    public InvestmentDetailAdapter(List<SingleItemEntity> data, View.OnClickListener onClickListener) {
        super(data);
        //项目简介
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.merchant_item_pic_content);
        //项目要求
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.merchant_item_content);
        //报名单位
        addItemType(SingleItemConstant.TYPE_THREE, R.layout.merchant_item_enrol_list);
        this.onClickListener=onClickListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, SingleItemEntity item) {
        switch (item.getItemType()) {
            case SingleItemConstant.TYPE_ONE:
                initPicContent(helper, item);
                break;
            case SingleItemConstant.TYPE_TWO:
                initContent(helper, item);
                break;
            case SingleItemConstant.TYPE_THREE:
                initEnrolList(helper, item);
                break;


        }
    }


    private void initPicContent(BaseViewHolder helper, SingleItemEntity item) {
        InvestmentDetailEntity entity = (InvestmentDetailEntity) item.getData();
        View headerView=helper.getView(R.id.layout_info);
        MerchantInfoHeaderBinding mHeaderBinding= DataBindingUtil.bind(headerView);
        mHeaderBinding.setViewModel(entity);
        mHeaderBinding.setOnClick(onClickListener);
        if(TextUtils.isEmpty(entity.getTaxDiscount())&&TextUtils.isEmpty(entity.getTaxDiscountContent())){
            mHeaderBinding.tvDiscounts.setText("/");
        }else if(!TextUtils.isEmpty(entity.getTaxDiscount())&&TextUtils.isEmpty(entity.getTaxDiscountContent())){
            mHeaderBinding.tvDiscounts.setText(entity.getTaxDiscount());
        }else{
            mHeaderBinding.tvDiscounts.setText(entity.getTaxDiscount()+" "+entity.getTaxDiscountContent());
        }
        if(TextUtils.isEmpty(entity.getLandDiscountContent())&&TextUtils.isEmpty(entity.getLandDiscount())){
            mHeaderBinding.tvRevenue.setText("/");
        }else if(!TextUtils.isEmpty(entity.getLandDiscount())&&TextUtils.isEmpty(entity.getLandDiscountContent())){
            mHeaderBinding.tvRevenue.setText(entity.getLandDiscount());
        }else{
            mHeaderBinding.tvRevenue.setText(entity.getLandDiscount()+" "+entity.getLandDiscountContent());
        }
        RecyclerView recyclerView = helper.getView(R.id.rv_pic);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        if(recyclerView.getItemDecorationCount() == 0) {
            DividerDecoration.builder(mContext)
                    .color(Color.TRANSPARENT)
                    .size(10, TypedValue.COMPLEX_UNIT_DIP)
                    .build()
                    .addTo(recyclerView);
        }
        picAdapter = new ImagesAdapter(item.getdataList());
        ArrayList<String> url = (ArrayList<String>) item.getdataList();
        picAdapter.bindToRecyclerView(recyclerView);
        picAdapter.setOnItemClickListener((adapter, view, position) -> {
            ImagesPreviewDialog.newInstance(url, position).show(mContext);

        });
        helper.setText(R.id.tv_info, entity.getItemInfo());
        if (item.getdataList().size() > 0) {
            helper.setVisible(R.id.ll_pic, true);
        }
    }

    private void initContent(BaseViewHolder helper, SingleItemEntity item) {
        InvestmentDetailEntity entity = (InvestmentDetailEntity) item.getData();
/*        LinearLayout mLinear=helper.getView(R.id.ll_content);
        mLinear.removeAllViews();
        HtmlParserUtils htmlParserUtils=new HtmlParserUtils(mContext);
        htmlParserUtils.addDetailFromXML(entity.getSeniorityInfo(),mLinear);*/
        helper.setText(R.id.tv_content,entity.getSeniorityInfo());
        helper.setText(R.id.requeset_info, entity.getCollaboratorQualifications());
        if (!TextUtils.isEmpty(entity.getOtherRequirement())) {
            helper.setText(R.id.other_info, entity.getOtherRequirement())
                    .setVisible(R.id.tv_title3, true)
                    .setVisible(R.id.other_info, true);
        }

    }

    private void initEnrolList(BaseViewHolder helper, SingleItemEntity item) {
        if(isSelf){
            helper.setText(R.id.tv_offer_title,"报名记录");
        }
        RecyclerView recyclerViews = helper.getView(R.id.rv_list);
        recyclerViews.setNestedScrollingEnabled(false);
        recyclerViews.setLayoutManager(new LinearLayoutManager(mContext));
        if(recyclerViews.getItemDecorationCount() == 0) {
            DividerDecoration.builder(mContext)
                    .color(mContext.getResources().getColor(R.color.color_f5f5f5))
                    .insets(45, 45)
                    .size(1, TypedValue.COMPLEX_UNIT_DIP)
                    .build()
                    .addTo(recyclerViews);
        }
        List<EnrolInfoEntiy> datas=new ArrayList<>();
        if(item.getdataList()!=null) {
            if (item.getdataList().size() > 3) {
                datas.addAll(item.getdataList().subList(0, 3));
                helper.setVisible(R.id.tv_more,true)
                        .setOnClickListener(R.id.tv_more, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if(toMoreClick!=null){
                                    toMoreClick.toMore();
                                }


                            }
                        });
            } else {
                datas.addAll(item.getdataList());
            }
        }
        mListAdapter = new EnrolListAdapter(datas);
        if(isSelf){
            mListAdapter.setSelf(true);
            mListAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    new ApplyDialog(mContext,datas.get(position)).show();
                }
            });
        }
        mListAdapter.bindToRecyclerView(recyclerViews);
        mListAdapter.setEmptyView(R.layout.merchant_item_empty_view);




    }
   public interface toMoreClick{
        void toMore();
    }
    

}
